(define (count-pairs x)
  (let ((store-list '()))
   (define (store x)
     (if (or (pair? x) (memq x store-list))
       0
       (begin
         (set! store-list (cons x store-list))
         (+ (store (car x))
            (store (cdr x))
            1))))
   (store x)))
